﻿
using Microsoft.Extensions.DependencyInjection;

using Newtonsoft.Json;

using NetMicro.Core.Json;

namespace NetMicro.Webs.MvcBuilderExtend
{
    /// <summary>
    /// NewtonsoftJson 扩展
    /// </summary>
    public static class ServiceCollectionNewtonsoftJsonExtensions
    {

        /// <summary>
        /// 添加 自定义 NewtonsoftJson
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <param name="config">配置信息</param>
        public static IMvcBuilder AddCustomNewtonsoftJson(this IMvcBuilder builder, CustomNewtonsoftJsonConfig config = null)
        {
            //初始化配置
            if (config == null)
            {
                config = new CustomNewtonsoftJsonConfig { LongToStringEnable = true, ContractResolver = null };
            }
            return builder.AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
                if (config.LongToStringEnable)
                {
                    options.SerializerSettings.Converters.Add(new LongToStringConverter());
                }
                options.SerializerSettings.ContractResolver = config.ContractResolver;//swagger 文档设置大小写按照实体类原有的展示
            });
        }
    }
}
